package sync;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.zip.Adler32;

// Modified version of the ChecksumAdler32 class
public class Checksum {
	
	public static long createChecksum(String filename) throws Exception {
		
		byte[] buffer = new byte[1024];
		Adler32 ad = new Adler32();
		InputStream in = new FileInputStream(filename);
		int numRead;
		
		int count = 0;
		do {
			numRead = in.read(buffer);
			if(numRead > 0) {
				ad.update(buffer, 0, numRead);
				++count;
			}
		} while(numRead != -1 && (count < 3200000));

		in.close();
		return ad.getValue();
	}
	
	public static boolean compateChecksum(String filename, long incommingChecksum) throws Exception {
		
		return createChecksum(filename) == incommingChecksum;
	}
}
